$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/yhbBenefit/list',
        datatype: "json",
        colModel: [
            {label: '分润id', name: 'yhbBenefitId', width: 45, key: true,align:"center"},
            {label: '姓名', name: 'username', width: 75,align:"center"},
            {label: '手机号码', name: 'phone', sortable: false, width: 75,align:"center"},
            {label: '交易额', name: 'allAmount', width: 85,align:"center"},
            {label: '分润', name: 'allTran', width: 60,align:"center"},
            {label: '分润时间', name: 'benefitDate', width: 90,align:"center"}

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        subGrid:true,
        subGridRowExpanded:function (subgrid_id,row_id) {
            bindSubGrid(subgrid_id, row_id)
        },
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

    vm.BindType();

});
var exmiane;

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            username: null,
            startTime: null,
            endTime:null,
            phone:null
        },
        showList: true,
        title: null,
        terminalTypes: [],

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";

        },

        update: function () {
            var userId = getSelectedRow();
            if (userId == null) {
                return;
            }

            vm.showList = false;
            vm.title = "修改";

        },
        reload: function () {

                if(( vm.q.startTime==null ||vm.q.startTime.length==0 )&& (vm.q.endTime!=null && vm.q.endTime!='')){
                    layer.msg("请选择开始时间");
                    return;
                }
                if((vm.q.startTime!=null && vm.q.startTime!='' ) && (vm.q.endTime==null||vm.q.endTime.length==0)){
                    layer.msg("请选择结束时间");
                    return;
                }





            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'username': vm.q.sn,'startTime':vm.q.startTime,'endTime':vm.q.endTime,'phone':vm.q.phone},
                page: page
            }).trigger("reloadGrid");
        },
        BindType:function () {
            $.get(baseURL+"sys/terminal/sysTerTypeList",r=>{
                vm.$set(vm.terminalTypes, 0, {
                    terminalType: 0,
                    terminalName: "请选择类型"
                });
                for (var i = 0; i < r.list.length; i++) {
                    vm.$set(vm.terminalTypes, (i + 1), r.list[i]);
                }


            })
        }
       
    }
});

//加载子表格数据
function bindSubGrid(subgrid_id, row_id) {
    // (2)子表格容器的id和需要展开子表格的行id，将传入此事件函数
    var subgrid_table_id;
    subgrid_table_id = subgrid_id + "_t";   // (3)根据subgrid_id定义对应的子表格的table的id

    var subgrid_pager_id;
    subgrid_pager_id = subgrid_id + "_pgr";  // (4)根据subgrid_id定义对应的子表格的pager的id
    var currRow=$("#jqGrid").jqGrid('getRowData',row_id);
    // (5)动态添加子报表的table和pager
    $("#" + subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+subgrid_pager_id+"' class='scroll'></div>");

    // (6)创建jqGrid对象
    $("#" + subgrid_table_id).jqGrid({
        url: baseURL + 'sys/yhbBenefit/detail',  // (7)子表格数据对应的url，注意传入的contact.id参数
        datatype: "json",
        postData:{'yhbBenefitId':currRow.yhbBenefitId},
        colModel: [
            { label: '终端名称', name: 'terminalName', width: 45,align:'center' , sortable: false},
            { label: '是否是vip', name: 'isVip', width: 45,align:'center' , sortable: false},
            { label: '交易额', name: 'tran', width: 45,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: '分润', name: 'amount', width: 45,align:'center' , sortable: false,formatter:function (value,options,row) {
                    return value.toFixed(2);
                }},
            { label: 'SN', name: 'sn', width: 100,align:'center' , sortable: false},
            { label: '终端号', name: 'merchantCode',width: 100,align:'center' , sortable: false}

        ],
        jsonReader: {   // (8)针对子表格的jsonReader设置
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
        },
        prmNames: {
            search: "search",
            page:"page",
            rows:"limit",
            order: "order"
        },
        pager: subgrid_pager_id,
        viewrecords: true,
        height: "100%",
        rowNum: 5
    }).setGridParam();

}